package github.ziwukun.jsonql.lang;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import github.ziwukun.jsonql.utils.CollectionUtils;

public class SortImpl implements Sort{
	private static final long serialVersionUID = -8085597931369925153L;
	List<PropSort> propSorts;
	
	SortImpl(List<PropSort> propSorts) {
		super();
		this.propSorts = propSorts;
		
		asserts();
	}

	@Override
	public List<PropSort> propSorts() {
		return new ArrayList<>(propSorts);
	}

	@Override
	public boolean isEmpty() {
		return propSorts.isEmpty();
	}
	
	@Override
	public boolean containProp(String propName) {
		if(StrUtil.isBlank(propName)) {
			return false;
		}
		return CollectionUtils.contains(propSorts, ps -> {
			return ps.prop().equals(propName);
		});
	}

	@Override
	public PropSort getPsByProp(String propName) {
		if(StrUtil.isBlank(propName)) {
			return null;
		}
		return CollectionUtils.findOne(propSorts, ps -> {
			return ps.prop().equals(propName);
		});
	}

	@Override
	public Sort repProp(String propName, String newName) {
		if(!containProp(propName)) {
			return ObjectUtil.cloneByStream(this);
		}
		if(containProp(newName)) {
			throw new JSONQLBuildException("新属性名称"+newName+"已存在");
		}
		
		// 寻找并替换
		List<PropSort> newPss = ObjectUtil.cloneByStream(this.propSorts);
		int propIndex = CollectionUtils.findFirstIndex(newPss, ps -> {
			return ps.prop().equals(propName);
		});
		PropSortImpl oldPs = (PropSortImpl)newPss.get(propIndex);
		PropSortImpl newPs = new PropSortImpl(newName, oldPs.val);
		newPss.set(propIndex, newPs);
		return new SortImpl(newPss);
	}

	@Override
	public Sort repProps(Map<String, String> propNames) {
		if(propNames == null || propNames.isEmpty()) {
			return ObjectUtil.cloneByStream(this);
		}
		
		Map<Integer, PropSort> newPss = new HashMap<>();
		for(Map.Entry<String, String> kv:propNames.entrySet()) {
			String oldProp = kv.getKey();
			String newProp = kv.getValue();
			
			// 不存在的属性 不做操作
			if(!containProp(oldProp)) {
				continue;
			}
			
			int propIndex = CollectionUtils.findFirstIndex(propSorts, ps -> {
				return ps.prop().equals(oldProp);
			});
			PropSortImpl oldPs = (PropSortImpl)propSorts.get(propIndex);
			PropSortImpl newPs = new PropSortImpl(newProp, oldPs.val);
			newPss.put(propIndex, newPs);
		}
		
		// 开始全量替换
		List<PropSort> newPsList = ObjectUtil.cloneByStream(propSorts);
		for(Map.Entry<Integer, PropSort> kv:newPss.entrySet()) {
			int index = kv.getKey();
			PropSort newPs = kv.getValue();
			newPsList.set(index, newPs);
		}
		return new SortImpl(newPsList);
	}

	@Override
	public Sort add(PropSort propSort) {
		List<PropSort> newPsList = ObjectUtil.cloneByStream(propSorts);
		newPsList.add(propSort);
		return new SortImpl(newPsList);
	}

	@Override
	public Sort addFirst(PropSort propSort) {
		LinkedList<PropSort> newPsList = new LinkedList<>(
			ObjectUtil.cloneByStream(propSorts));
		newPsList.addFirst(propSort);
		return new SortImpl(newPsList);
	}
	
	@Override
	public String toJSONQL() {
		return SortJson.writeJson(this);
	}
	
	private void asserts() {
		if(propSorts == null) {
			propSorts = new ArrayList<>();
		}
		for(PropSort propSort:propSorts) {
			for(PropSort ps:propSorts) {
				if(propSort != ps && propSort.prop().equals(ps.prop())) {
					throw new JSONQLBuildException("不能存在重复的属性名称" + ps.prop());
				}
			}
		}
	}
}
